package com.hfjyz.registration.validator;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.hfjyz.registration.command.AnnouncementEditCommand;
import com.hfjyz.registration.contants.ExamContants;

public class AnnouncementEditValidator implements Validator {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(AnnouncementEditCommand.class);
	}

	public void validate(Object target, Errors errors) {
		validateRoomInfo(target, errors);
	}

	private void validateRoomInfo(Object target, Errors errors) {
		AnnouncementEditCommand v = (AnnouncementEditCommand) target;
		if (v.getName() == null || v.getName().length() == 0) {
			errors.rejectValue("name", "required.name", "名称必须填写!");
		}

		// Calendar now = Calendar.getInstance();
		// now.set(Calendar.HOUR_OF_DAY, 0);
		// now.set(Calendar.MINUTE, 0);
		// now.set(Calendar.SECOND, 0);
		// now.add(Calendar.DAY_OF_YEAR, 1);
		// Date today = now.getTime();
		SimpleDateFormat timeSf = new SimpleDateFormat(
				ExamContants.YYYY_MM_DD_HH_MM);
		if (v.getPublishDate() == null || v.getPublishDate().length() == 0) {
			errors.rejectValue("publishDate", "required.publishDate",
					"发布日期必须填写");
		} else if (!isDate(v.getPublishDate(), timeSf)) {
			errors.rejectValue("publishDate", "required.publishDate", "发布日期不正确");
		}
		if (v.getApplyBeginDate() == null
				|| v.getApplyBeginDate().length() == 0) {
			errors.rejectValue("applyBeginDate", "required.applyBeginDate",
					"报名开始时间必须填写");
		} else if (!isDate(v.getApplyBeginDate(), timeSf)) {
			errors.rejectValue("applyBeginDate", "required.applyBeginDate",
					"报名开始时间格式不正确");
		} else {
			// try {
			// Date d = timeSf.parse(v.getApplyBeginDate());
			// if (d.before(today)) {
			// errors.rejectValue("applyBeginDate",
			// "required.applyBeginDate", "报名开始时间必须在今天之后");
			// }
			// } catch (ParseException e) {
			// }
		}
		if (v.getApplyDeadDate() == null || v.getApplyDeadDate().length() == 0) {
			errors.rejectValue("applyDeadDate", "required.applyDeadDate",
					"报名结束时间必须填写");
		} else if (!isDate(v.getApplyDeadDate(), timeSf)) {
			errors.rejectValue("applyDeadDate", "required.applyDeadDate",
					"报名结束时间格式不正确");
		} else {
			// try {
			// Date d = timeSf.parse(v.getApplyDeadDate());
			// if (d.before(today)) {
			// errors.rejectValue("applyDeadDate",
			// "required.applyDeadDate", "报名结束时间必须在今天之后");
			// }
			// } catch (ParseException e) {
			// }
		}
		if (v.getAdmissionPrintStart() == null
				|| v.getAdmissionPrintStart().length() == 0) {
			errors.rejectValue("admissionPrintStart",
					"required.admissionPrintStart", "准考证打印开始时间必须填写");
		} else if (!isDate(v.getAdmissionPrintStart(), timeSf)) {
			errors.rejectValue("admissionPrintStart",
					"required.admissionPrintStart", "准考证打印开始时间格式不正确");
		}
		if (v.getAdmissionPrintEnd() == null
				|| v.getAdmissionPrintEnd().length() == 0) {
			errors.rejectValue("admissionPrintEnd",
					"required.admissionPrintEnd", "准考证打印结束时间必须填写");
		} else if (!isDate(v.getAdmissionPrintEnd(), timeSf)) {
			errors.rejectValue("admissionPrintEnd",
					"required.admissionPrintEnd", "准考证打印结束时间格式不正确");
		}
		SimpleDateFormat dateSf = new SimpleDateFormat(ExamContants.YYYY_MM_DD);
		if (v.getExamDate() == null || v.getExamDate().length() == 0) {
			errors.rejectValue("examDate", "required.examDate", "考试日期必须填写");
		} else if (!isDate(v.getExamDate(), dateSf)) {
			errors.rejectValue("examDate", "required.examDate", "考试日期格式不正确");
		}
		if (v.getExamTime() == null || v.getExamTime().length() == 0) {
			errors.rejectValue("examTime", "required.examTime", "考试时间必须填写");
		} else if (!v.getExamTime().matches(
				"^([0-1]\\d|2[0-3]):[0-5]\\d~([0-1]\\d|2[0-3]):[0-5]\\d$")) {
			errors.rejectValue("examTime", "required.examTime", "考试时间格式不正确");
		}

		if (v.isOnlinePay()) {
			if (v.getRegFee() == null || v.getRegFee().length() == 0) {
				errors.rejectValue("regFee", "required.regFee", "在线支付时必须填写报名费");
			} else {

				try {
					Double.parseDouble(v.getRegFee());
				} catch (NumberFormatException e) {
					errors.rejectValue("regFee", "regFee.error", "报名费格式不正确");
				}
			}
			if (v.getPayStartDate() == null || v.getPayStartDate().length() == 0) {
				errors.rejectValue("payStartDate", "required.payStartDate",
						"在线支付时支付开始时间必须填写");
			} else if (!isDate(v.getPayStartDate(), timeSf)) {
				errors.rejectValue("payStartDate", "required.payStartDate", "支付开始时间不正确");
			}
			if (v.getPayEndDate() == null || v.getPayEndDate().length() == 0) {
				errors.rejectValue("payEndDate", "required.payEndDate",
						"在线支付时支付结束时间必须填写");
			} else if (!isDate(v.getPayEndDate(), timeSf)) {
				errors.rejectValue("payEndDate", "required.payEndDate", "支付结束时间不正确");
			}
		}

	}

	public boolean isDigital(String str) {
		return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
	}

	private boolean isDate(String str, SimpleDateFormat sf) {
		if (str == null) {
			return false;
		}
		try {
			sf.parse(str);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

}
